import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JToggleButton;


public class Tiles implements ActionListener{
	
	
	ViewerMapaMain vmp;
	ViewerOverMap vom;
	BufferedImage mapTile;
	BufferedImage mapTiles[][];
	JToggleButton btnsTile[][];
	ImageIcon icones[][];
	int l,h,count=1,apertos=0;
	JToggleButton portal;
	JToggleButton tBtnold;
	JToggleButton tBtn;
	
	
public Tiles() {

}

public Tiles(ViewerMapaMain vmp) throws IOException  {
	this.vmp = vmp;
	if(vmp.m != null){
		vmp.m.RefazerVMP();
	}
	vmp.painelTile.removeAll();
	
	vmp.painelTile.add(vmp.painelTile.add(vmp.getMap));
	mapTile = Utils.readImage( vmp.getPath() ); 
	portal = new JToggleButton("Portal");
	portal.setToolTipText("Portal");
	portal.addActionListener(this);
	vmp.painelTile.add(portal);
	l = mapTile.getWidth()/32;
	h = mapTile.getHeight()/32;
	mapTiles = Utils.splitImage(mapTile, h, l);
	btnsTile = new JToggleButton[h][l];
	icones = new ImageIcon[h][l];
	for (int i = 0; i < mapTiles.length; i++) {
        for (int x = 0; x < mapTiles[i].length; x++) {
        	icones[i][x] = new ImageIcon(mapTiles[i][x]); 
        	btnsTile[i][x] = new JToggleButton();
        	btnsTile[i][x].setToolTipText(trans(count));
        	btnsTile[i][x].setIcon(icones[i][x]);
        	btnsTile[i][x].setMargin(new Insets(0, 0, 0, 0));
        	btnsTile[i][x].addActionListener(this);
        	btnsTile[i][x].setPreferredSize(new Dimension(40,40));
        	vmp.painelTile.add(btnsTile[i][x]);
        	count++;
        }
	}
	
	
	vmp.painelTile.validate();
}

public Tiles(ViewerOverMap vom) throws IOException {
	this.vom = vom;
	if(vom.m != null){
		vom.m.RefazerVOM();
		//System.out.println("?");
	}
	//System.out.println("?");
	vom.painelTile.removeAll();
	
	vom.painelTile.add(vom.painelTile.add(vom.getMap));
	mapTile = Utils.readImage( vom.getPath() ); 
	l = mapTile.getWidth()/32;
	h = mapTile.getHeight()/32;
	mapTiles = Utils.splitImage(mapTile, h, l);
	btnsTile = new JToggleButton[h][l];
	icones = new ImageIcon[h][l];
	for (int i = 0; i < mapTiles.length; i++) {
        for (int x = 0; x < mapTiles[i].length; x++) {
        	icones[i][x] = new ImageIcon(mapTiles[i][x]); 
        	btnsTile[i][x] = new JToggleButton();
        	btnsTile[i][x].setToolTipText(trans(count));
        	btnsTile[i][x].setIcon(icones[i][x]);
        	btnsTile[i][x].setMargin(new Insets(0, 0, 0, 0));
        	btnsTile[i][x].addActionListener(this);
        	btnsTile[i][x].setPreferredSize(new Dimension(40,40));
        	vom.painelTile.add(btnsTile[i][x]);
        	count++;
        }
	}
	
	
	vom.painelTile.validate();
}


@Override
public void actionPerformed(ActionEvent e) {
	tBtn = (JToggleButton)e.getSource();
	if(tBtn.getToolTipText().equals("Portal")){
		
	}else if (tBtn.isSelected() && apertos > 0) {	
	    	tBtnold.setSelected(false);
	    	} 
	    tBtnold = tBtn;
	    apertos++;

}

public JToggleButton getBtn() {
	return tBtn;
}

public static String trans(int n) {
	if (n <= 9)
		return "0" + n;
	else
		return String.valueOf(n);
}


}
